﻿/**
* jQuery EasyUI 1.4.3
* Copyright (c) 2009-2015 www.jeasyui.com. All rights reserved.
*
* Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
* To use it on other terms please contact us at info@jeasyui.com
* http://www.jeasyui.com/license_commercial.php
*
* jQuery EasyUI combobox 扩展
* jeasyui.extensions.combobox.autoSelectFirst.js
* 开发 落阳
* 最近更新：2019-11-19
*
* 依赖项：
*   1、jquery.jdirk.js
*
* Copyright (c) 2015 ChenJianwei personal All rights reserved.
*/
(function ($) {

    $.util.namespace("$.fn.combobox.extensions");
    

    function resetExtensionsEvent(t, opts) {
        if (!opts.autoSelectFirst) { return; }

        var _onLoadSuccess = opts.onLoadSuccess;
        opts.onLoadSuccess = function (data) {
            if ($.isFunction(_onLoadSuccess)) { _onLoadSuccess.apply(this, arguments); }
            if (opts.autoSelectFirst && data && data.length) {
                var the = data[0];
                $(this).combobox("setValue", the[opts.valueField]);
            }
        };
    }

    function initializeExtensions(target) {
        var t = $(target),
            state = $.data(target, "combobox"),
            opts = state.options;
        resetExtensionsEvent(t, opts);
    }

    var _combobox = $.fn.combobox;
    $.fn.combobox = function (options, param) {
        if (typeof options == "string") {
            return _combobox.apply(this, arguments);
        }
        options = options || {};
        return this.each(function () {
            var jq = $(this),
                isInited = $.data(this, "combobox") ? true : false,
                opts = isInited ? options : $.extend({},
                        $.fn.combobox.parseOptions(this),
                        $.parser.parseOptions(this, [
                            {
                                autoSelectFirst: "boolean"
                            }
                        ]), options);
            _combobox.call(jq, opts, param);
            if (!isInited) {
                initializeExtensions(this);
            }
        });
    };
    $.union($.fn.combobox, _combobox);





    var defaults = {

        // 扩展 easyui-combobox 的自定义属性，表示远程数据加载完成后是否自动选择第一个选项；
        // Boolean 值，默认为 false。
        // 注意：使用本地数据(data)作为数据源时，本扩展无效。因为本地数据源不会经过 onLoadSuccess.
        autoSelectFirst: false
    };

    var methods = {

    };

    if ($.fn.combobox.extensions.defaults) {
        $.extend($.fn.combobox.extensions.defaults, defaults);
    } else {
        $.fn.combobox.extensions.defaults = defaults;
    }

    if ($.fn.combobox.extensions.methods) {
        $.extend($.fn.combobox.extensions.methods, methods);
    } else {
        $.fn.combobox.extensions.methods = methods;
    }

    $.extend($.fn.combobox.defaults, defaults);
    $.extend($.fn.combobox.methods, methods);

})(jQuery);